package com.mohansever.controller;


import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Null;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.mohansever.util.PageData;
import com.mohansever.util.ResponseUtil;

public class BaseController {
	
	protected Logger logger = Logger.getLogger(this.getClass());
	/**
	 * 得到PageData
	 */
	public PageData getPageData(){
		return new PageData(this.getRequest());
	}
	
	/**
	 * 得到ModelAndView         
	 */
	public ModelAndView getModelAndView(){
		return new ModelAndView();
	}
	
	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}
 
	 
	/***
	 * 支持异步跨域请假  callback函数 
	 */
	public static Object returnObject(PageData pd, Map<String, Object> map){
		if(pd.containsKey("callback")){
			String callback = pd.get("callback").toString();
			return new JSONPObject(callback, map);
		}else{
			return map;
		}
	}
	
	public static Object returnObject(PageData pd, ResponseUtil ru){
		if(pd.containsKey("callback")){
			String callback = pd.get("callback").toString();
			
			return new JSONPObject(callback, ru);
		}else{
			return ru;
		}
	}

	public static boolean parmIsEnable(Map<String, Object> map){
		for (Map.Entry<String, Object> entry : map.entrySet()) {
		    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
		    if (entry.getValue() == null || "".equals(entry.getValue().toString())) {
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean validataParms(String [] pkey , PageData pd ){
		boolean flag = true;
		try {
			for (String string : pkey) {
				System.out.println(string + "=");
				System.out.println(pd.get(string));
				if (pd.get(string) == null || "".equals(pd.get(string).toString())) {
					flag = false;
					break;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;	
	}
	
	public String getRemoteHost(javax.servlet.http.HttpServletRequest request){
	    String ip = request.getHeader("x-forwarded-for");
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
	        ip = request.getHeader("Proxy-Client-IP");
	    }
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
	        ip = request.getHeader("WL-Proxy-Client-IP");
	    }
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
	        ip = request.getRemoteAddr();
	    }
	    return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
	}
	
	public static long getUserID(){
		Date d = new Date();
		return d.getTime();
		
	}
	
	public static int getRandom(int max , int min){
		int x;//定义两变�?
        Random ne=new Random();//实例化一个random的对象ne
        x=ne.nextInt(max-min+1)+min;//为变量赋随机�?1000-9999
        return x;
	}
	/**
	 * 
	 * @Title: getRandomSix   
	 * @Description: TODO(获取6位数字的随机�?)   
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int getRandomSix(){
		int x;//定义两变�?
		Random ne=new Random();//实例化一个random的对象ne
		x=ne.nextInt(999999-100000+1)+100000;//为变量赋随机�?1000-9999
		return x;
	}
	
}
